package com.estate.owner.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.estate.bean.Owner;
import com.estate.bean.Quarters;
import com.estate.owner.service.OwnerService;
import com.estate.util.JsonResult;
import com.estate.util.ResultTool;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/owner")
public class OwnerController {
    @Resource
    private OwnerService ownerService;


    @GetMapping("/listByPage/{page}/{size}")
    public JsonResult listByPage(@PathVariable("page") int page, @PathVariable("size") int size) {
        log.info("page:{},size:{}", page, size);
        JsonResult result = ownerService.listByPage(page, size);
        log.info("result:{}", result);
        return ResultTool.success(result);
    }

    @PostMapping("/save")
    public JsonResult save(Owner owner) {
        log.info("owner:{}", owner);
        JsonResult result = ownerService.addOwner(owner);
        log.info("result:{}", result);
        return ResultTool.success(result);
    }

    @PutMapping("/update")
    public JsonResult update(Owner owner) {
        log.info("owner:{}", owner);
        JsonResult result = ownerService.updateOwner(owner);
        log.info("result:{}", result);
        return ResultTool.success(result);
    }

    @DeleteMapping("/delete")
    public JsonResult delete(String id) {
        log.info("id:{}", id);
        return ownerService.deleteOwner(id);
    }

    @GetMapping("/list/{ownerId}")
    public JsonResult list(@PathVariable("ownerId") String ownerId) {
        log.info("ownerId:{}", ownerId);
        JsonResult result = ResultTool.success(ownerService.findById(ownerId));
        log.info("result:{}", result);
        return result;
    }

    @GetMapping("/findByCommunityId/{communityId}")
    public Long findByCommunityId(@PathVariable("communityId") String communityId) {
        log.info("communityId:{}", communityId);
        QueryWrapper<Owner> wrapper=new QueryWrapper();
        wrapper.eq("community_id",communityId);
        long count = ownerService.count(wrapper);
        log.info("result:{}", count);
        return count;
    }
}
